<template>
  <div class="searchIndex">
    <!-- 搜索框 -->

    <div class="searchbox">
      <span class="searchicon"> <van-icon name="arrow-left" @click="back" /> </span>
      <van-search v-model="searchValue" placeholder="请输入搜索关键词" @click-input="show = true" />
      <span class="search" @click="handSearch">搜索</span>
    </div>

    <!-- 历史搜索 -->
    <div class="history" v-show="show==true">

      <div class="recent_searches">
        <span>最近搜索</span>
        <!-- 点击删除svg删除全部搜索历史 -->
        <span @click="celarhistory"
          ><svg
            t="1733984369024"
            class="icon"
            viewBox="0 0 1024 1024"
            version="1.1"
            xmlns="http://www.w3.org/2000/svg"
            p-id="14203"
            width="200"
            height="200"
          >
            <path
              d="M768 153.6h230.4a25.6 25.6 0 1 1 0 51.2H921.6v716.8a102.4 102.4 0 0 1-102.4 102.4H204.8a102.4 102.4 0 0 1-102.4-102.4V204.8H25.6a25.6 25.6 0 0 1 0-51.2H256V102.4a102.4 102.4 0 0 1 102.4-102.4h307.2a102.4 102.4 0 0 1 102.4 102.4v51.2z m-51.2 0V102.4a51.2 51.2 0 0 0-51.2-51.2H358.4a51.2 51.2 0 0 0-51.2 51.2v51.2h409.6zM153.6 204.8v716.8a51.2 51.2 0 0 0 51.2 51.2h614.4a51.2 51.2 0 0 0 51.2-51.2V204.8H153.6z"
              fill="#b0b0b0"
              p-id="14204"
            ></path></svg
        ></span>
      </div>

      <button class="historybtn" v-for="(item, index) in historyVal" :key="index">
        <span @click="historySearch(item)">{{ item }}</span>
      </button>
    </div>
    <div v-show="show==false">
      <div v-show="searchlist==''">
        <van-empty description="没有搜索到相关的内容哦"/>
      </div>
      <div v-show="searchlist">
        <div
        class="recommend-list"
        v-for="item in searchlist"
        :key="item.id"
        @click="toDetail(item)"
      >
        <div class="Dtop">
          <img :src="item.avatar" alt="" />
          <div class="Dtop_left">
            <p>{{ item.name }}</p>
            <p style="color: #c0c0c0">
              <span>{{ item.position }}</span
              >&nbsp;<span>{{ item.time }}</span>
            </p>
          </div>
        </div>
        <div class="Dmain" v-show="item.type === '视频'">
          <div class="type1">
            <span class="type" style="background-color: #a0a8f8">{{ item.type }}</span>
            <span class="typebotom">{{ item.title }}</span>
          </div>
          <div class="video">
            <img class="video" v-lazy="item.img">
          </div>
          <van-text-ellipsis rows="1" :content="item.desc" />
        </div>
        <div class="Dmain" v-show="item.type === '问答'">
          <div class="type1">
            <span class="type" style="background-color: #38d1af">{{ item.type }}</span>
            <span class="typebotom">{{ item.title }}</span>
          </div>
          <van-text-ellipsis rows="1" :content="item.desc" />
        </div>
        <div class="Dmain" v-show="item.type === '音频'">
          <div class="type1">
            <span class="type" style="background-color: #7ed6ee">{{ item.type }}</span>
            <span class="typebotom">{{ item.title }}</span>
          </div>
          <div class="audio">
            <img v-lazy="item.mp3">
          </div>
          <van-text-ellipsis rows="1" :content="item.desc" />
        </div>
        <div class="Dmain" v-show="item.type === '文章'">
          <div class="type1">
            <span class="type" style="background-color: #43d3b4">{{ item.type }}</span>
            <span class="typebotom">{{ item.title }}</span>
          </div>
          <van-text-ellipsis rows="1" :content="item.desc" />
        </div>
      </div>
      </div>
     
    </div>
  </div>
</template>
<script setup lang="ts">
import './index.css'
import { useRouter } from "vue-router";
import { ref } from "vue";
import axios from "axios";
const searchValue = ref("");
const show = ref(true);

// 搜索结果

// 往pinia存搜索框的数据
const historyVal = ref(
  localStorage.getItem("history")
    ? JSON.parse(localStorage.getItem("history") as string)
    : []
);
const router = useRouter();
// 点击返回上级
const back = () => {
  router.go(-1);
};
// 点击删除icon图标删除搜索历史全部数据
const celarhistory = () => {
  historyVal.value = [];
  localStorage.removeItem("history");
};
// 搜索之后的数据
let searchlist = ref()
const handSearch = () => {
  if (searchValue.value == "") {
    alert("请输入搜索内容");
  } else {
    historyVal.value.unshift(searchValue.value);
    localStorage.setItem("history", JSON.stringify(historyVal.value));
    axios
      .get("/api/DownDoctorList", { params: { searchValue: searchValue.value } })
      .then((res) => {
        const {data,length} = res.data;
        searchlist.value = data
        show.value = false;
      });
    searchValue.value = "";
  }
};
const historySearch = (item) => {
  // 点击搜索历史内容将搜索的数据调用
  axios.get("/api/DownDoctorList", { params: { searchValue: item } }).then((res) => {
    // 将搜索完成之后的数据存到searchVal中
    const {data,length} = res.data;
    console.log(res.data.data);
        searchlist.value = data 
        show.value = false;
  });
};
const toDetail = (item) => {
  const val = encodeURIComponent(JSON.stringify(item));
  router.push(`/recdetail?item=` + val);
};
</script>
